package com.tengxun.api.yuyin;// 创建AsrClient对象并调用CreateRecTask
import com.tencentcloudapi.asr.v20190614.AsrClient;
import com.tencentcloudapi.asr.v20190614.models.CreateRecTaskRequest;
import com.tencentcloudapi.asr.v20190614.models.CreateRecTaskResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

public class AsrTaskExample {
    //TaskId: 12765515368
    //RequestId: be631c3f-cee0-4daf-bacf-a5e413677191
    public static void main(String[] args) throws IOException {
        try {
            // 1. 创建认证信息
            Credential cred = new Credential("AKIDfDYfau0rXzDDyZ9czKivwleJkxzAFdtw", "vgFZfXBamkU2ZtzwsfO3bx5HMGUbYKsC");
            
            // 2. 配置HTTP选项
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("asr.tencentcloudapi.com");
            
            // 3. 配置客户端选项
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            
            // 4. 创建AsrClient对象
            AsrClient client = new AsrClient(cred, "ap-beijing", clientProfile);
            
            // 5. 创建CreateRecTask请求
            CreateRecTaskRequest req = new CreateRecTaskRequest();
            req.setEngineModelType("16k_zh"); // 识别引擎类型
            req.setChannelNum(1L); // 音频声道数
            req.setResTextFormat(0L); // 结果返回方式
            req.setSourceType(0L); // 音频来源
            req.setUrl("https://test-static-community.zhijianiot.com/assets/shequbao/ai/ai.mp3"); // 音频URL
//            String audioFilePath = "C:\\Users\\11831\\Desktop\\启基永昌南京研发中心 2.m4a"; // 替换为实际音频文件路径
//            String base64AudioData = fileToBase64Safe(audioFilePath);
//            req.setData(base64AudioData); // 设置Base64编码的音频数据
            // 6. 调用CreateRecTask接口
            CreateRecTaskResponse resp = client.CreateRecTask(req);
            
            // 7. 处理响应结果
            System.out.println("TaskId: " + resp.getData().getTaskId());
            System.out.println("RequestId: " + resp.getRequestId());
            
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

    /**
     * 更安全的文件转Base64方法，适用于大文件
     * @param filePath 文件路径
     * @return Base64编码字符串
     * @throws IOException 文件读取异常
     */
    public static String fileToBase64Safe(String filePath) throws IOException {
        File file = new File(filePath);
        byte[] fileBytes = new byte[(int) file.length()];

        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            int bytesRead = 0;
            int offset = 0;
            while (offset < fileBytes.length && (bytesRead = fileInputStream.read(fileBytes, offset, fileBytes.length - offset)) >= 0) {
                offset += bytesRead;
            }
        }

        return Base64.getEncoder().encodeToString(fileBytes);
    }

}
